﻿/*******************************************************
 * 
 * 作者：胡庆访
 * 创建时间：20100326
 * 说明：用于加载元数据的模块基类
 * 运行环境：.NET 3.5 SP1
 * 版本号：1.0.0
 * 
 * 历史记录：
 * 创建文件 胡庆访 20100326
 * 
*******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.Command;
using OpenExpressApp.Module;
using OpenExpressApp.MetaModel;
using System.IO;
using System.Collections.ObjectModel;
using OpenExpressApp.Library;

namespace OpenExpressApp
{
    /// <summary>
    /// 用于加载元数据的模块基类
    /// 
    /// 加入业务模型 命令等
    /// </summary>
    public abstract class LoadMetaDataModule : IModule
    {
        #region IModule Members

        /// <summary>
        /// 加入Module类在程序集内的 业务模型 命令等
        /// </summary>
        public virtual void Initialize()
        {
            foreach (var item in this.GetEntityTypes())
            {
                //加入业务模型
                BusinessObjectInfo bOInfo = ApplicationModel.AddBusinessObject(item);
                //为业务模型加入默认命令
                bOInfo.SetDefaultCommands();
            }

            foreach (var item in this.GetCommandTypes())
            {
                //加入自定义命令
                var command = Activator.CreateInstance(item) as CommandBase;
                ApplicationModel.Commands.Add(command);
                if (item.HasMarked<InPermissionAttribute>())
                {
                    InPermissionAttribute attr = item.GetSingleAttribute<InPermissionAttribute>();
                    ApplicationModel.TypeOwnerInPermission.Add(attr.Child, attr.Owner);
                }
            }
        }

        #endregion

        /// <summary>
        /// 获取当前模块的所有命令类型
        /// </summary>
        /// <returns></returns>
        protected IEnumerable<Type> GetCommandTypes()
        {
            return GetTypeMarked<CommandAttribute>(this.GetType().Assembly);
        }

        /// <summary>
        /// 获取指定模块的所有命令类型
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        protected IEnumerable<Type> GetCommandTypes(Assembly assembly)
        {
            return GetTypeMarked<CommandAttribute>(assembly);
        }

        /// <summary>
        /// 获取当前模块的所有实体类型
        /// </summary>
        /// <returns></returns>
        protected IEnumerable<Type> GetEntityTypes()
        {
            return GetTypeMarked<BusinessObjectAttribute>(this.GetType().Assembly);
        }

        /// <summary>
        /// 获取指定模块的所有实体类型
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        protected IEnumerable<Type> GetEntityTypes(Assembly assembly)
        {
            return GetTypeMarked<BusinessObjectAttribute>(assembly);
        }

        private static IEnumerable<Type> GetTypeMarked<TAttr>(Assembly assembly) where TAttr : Attribute
        {
            return assembly.GetTypes().Where(t => t.HasMarked<TAttr>());
        }
    }
}
